//
//  NewsDetailViewController.m
//  SampleApp2
//
//  Created by GXHC on 2023/6/7.
//

#import "NewsDetailViewController.h"
#import <WebKit/WebKit.h>
@interface NewsDetailViewController ()<WKNavigationDelegate>
@property(nonatomic, strong, readwrite) WKWebView *webview;
@property(nonatomic, strong, readwrite) UIProgressView *progressView;
@end

@implementation NewsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	//self.navigationItem.backButtonTitle=@"返回";
	self.webview = [[WKWebView alloc]init];
	self.webview.navigationDelegate = self;
	self.webview.frame = CGRectMake(0, 88, self.view.frame.size.width, self.view.frame.size.height-80);
	[self.view addSubview:self.webview];
	[self.view addSubview:({
		self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 88, self.view.frame.size.width, 20)];
		self.progressView;
	})];
	
	
	[self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
	[self.webview addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
	
	
}

- (void)dealloc{
	[self.webview removeObserver:self forKeyPath:@"estimatedProgress"];
	NSLog(@"");
}

- (void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary<NSKeyValueChangeKey, id> *)change context:(nullable void *)context{
	NSLog(@"");
	self.progressView.progress = self.webview.estimatedProgress;
}

#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
	decisionHandler(WKNavigationActionPolicyAllow);
	
	NSLog(@"加载中");
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
	NSLog(@"加载完成");
	[self.progressView removeFromSuperview];
}


- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error{
	NSLog(@"加载失败");
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
